package com.app.ytong.home;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.ytong.R;
import com.app.ytong.bean.BaseInfoUpdateCache;
import com.app.ytong.bean.LoginBean;
import com.app.ytong.bean.MenuBean;
import com.app.ytong.dialog.UpdateBaseInfoDialog;
import com.app.ytong.greendao.GreenDaoUtils;
import com.app.ytong.login.LoginActivity;
import com.app.ytong.manager.PutTabActivity;
import com.app.ytong.other.PhotoDialog;
import com.app.ytong.set.SettingListActivity;
import com.app.ytong.util.MyUtils;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.ActivityTool;
import my.SystemParamsUtils;

/**
 * 首页
 */
public class HomeIndexActivity extends HomeBaseActivity {


    @BindView(R.id.text_compay_name)
    TextView text_compay_name;
    @BindView(R.id.text_scale_id)
    TextView text_scale_id;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    GridAdapter adapter;

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.home_index_activity);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        setTitleBg(R.color.transparent);
        QMUIStatusBarHelper.setStatusBarDarkMode(mContext);

        try {
            text_compay_name.setText(LoginBean.getInstance().getAname());
            text_compay_name.setVisibility(View.VISIBLE);
        }catch (Exception e){
            LoginActivity.toLoginAllClear(mContext);
            throw e;
        }
        text_scale_id.setVisibility(View.VISIBLE);
        text_scale_id.setText(SystemParamsUtils.getAndroidID(mContext));
        text_scale_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoDialog.toImgScanDialog(mContext,CodeUtils.createImage(SystemParamsUtils.getAndroidID(mContext), 400, 400, null));
            }
        });
        checkCacheAndDownload();

        recycler.setLayoutManager(new GridLayoutManager(mContext, 2));
        adapter = new GridAdapter();
        recycler.setAdapter(adapter);
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);

    }


    @Override
    protected void onResume() {
        super.onResume();
//        QMUIStatusBarHelper.setStatusBarDarkMode(mContext);
    }


    @Override
    public void onDoubleClick() {
        super.onDoubleClick();
        refreshData();
    }

    void refreshData() {
    }



    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            refreshData();
        }
    }



    void checkCacheAndDownload() {
        GreenDaoUtils.clearAll();
        BaseInfoUpdateCache beanNew  = new BaseInfoUpdateCache();
        UpdateBaseInfoDialog.toBaseInfoUpdateDialog(mContext, beanNew);
//        MySharedPreferences msp = new MySharedPreferences(mContext);
//        if (msp.isFirst(mContext)) {
//            LogUtil.i("第一次打开  或者 通讯录缓存不存在--直接更新->");
//            msp.setFirstDone(mContext);
//        } else {
//            msp.setFirstDone(mContext);
//        }
    }

    @OnClick({R.id.view_set})
    public void btnClick(View v){
        switch (v.getId()){
            case R.id.view_set:
                ActivityTool.skipActivity(mContext, SettingListActivity.class);
                break;
        }

    }


    public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {


        //创建新View，被LayoutManager所调用
        @Override
        public GridAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.home_enter_grid_item, viewGroup, false);
            return new GridAdapter.ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(GridAdapter.ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return LoginBean.getInstance() == null ? 0 : LoginBean.getInstance().getRightTrue().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.img)
            public ImageView img;

            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                MenuBean data = LoginBean.getInstance().getRightTrue().get(position);
                text_name.setText(data.getMna());
                switch (position){
                    case 0:
                        img.setImageResource(R.mipmap.icon_tea);
                        break;
                    case 1:
                        img.setImageResource(R.mipmap.icon_tea_mao);
                        break;
                    case 2:
                        img.setImageResource(R.mipmap.icon_tea_bing);
                        break;
                    case 3:
                        img.setImageResource(R.mipmap.icon_tea_sang);
                        break;
                }

                list_item.setBackground(getResources().getDrawable(data.getBackGround(position)));
                list_item.setTag(data);
            }

            @OnClick(R.id.list_item)
            public void onItemClick(View v) {
                MenuBean  data = (MenuBean) v.getTag();
                if(!data.isSele()){
                    showToast("权限不足");
                    return;
                }

                MyUtils.putHomeSelectId(data.getId());
                MyUtils.putHomeSelectName(data.getMna());
                ActivityTool.skipActivity(mContext, PutTabActivity.class);
            }
        }
    }


}
